package com.summer.campussct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.campussct.mapper.RoleMapper;
import com.summer.campussct.mapper.RolePermissionMapper;
import com.summer.campussct.mapper.UserMapper;
import com.summer.campussct.pojo.Role;
import com.summer.campussct.pojo.RolePermission;
import com.summer.campussct.pojo.User;
import com.summer.campussct.pojo.dto.RestResult;
import com.summer.campussct.service.IRoleService;
import com.summer.campussct.utils.UserIdHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2024-05-07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public RestResult getList() {
        List<Role> list = this.list(new LambdaQueryWrapper<Role>()
                .select(Role::getId, Role::getRoleCode ,Role::getRoleName, Role::getRoleDesc, Role::getStatus));
        return RestResult.success(list);
    }

    @Override
    public RestResult getListEnabled() {
        List<Role> list = this.list(new LambdaQueryWrapper<Role>()
                .select(Role::getId ,Role::getRoleName)
                .eq(Role::getStatus, 1));
        return RestResult.success(list);
    }

    @Override
    public RestResult addRole(Role role) {
        if (!this.save(role)) {
            throw new RuntimeException("添加角色失败");
        }
        return RestResult.success();
    }

    @Override
    public RestResult modifyRole(Role role, long id) {
        role.setId(id);
        if (!this.updateById(role)) {
            throw new RuntimeException("修改角色失败");
        }
        return RestResult.success();
    }

    @Override
    @Transactional
    public RestResult deleteRole(long id) {
        // 删除角色表
        if (!this.removeById(id)) {
            throw new RuntimeException("删除角色失败");
        }
        // 删除关联表
        int delete = rolePermissionMapper.delete(new LambdaQueryWrapper<RolePermission>()
                .eq(RolePermission::getRid, id));
        if (delete == 0){
            throw new RuntimeException("删除关联表失败");
        }
        //todo 待测试：将user中的roleId置空 update user set role_id = null where role_id = ?
        userMapper.updateRoleId(id);
        return RestResult.success();
    }

    @Override
    public RestResult rolePermList(long id) {
        List<Integer> permIdList = rolePermissionMapper.getIdList(id);
        return RestResult.success(permIdList);
    }

    @Override
    @Transactional
    public RestResult assignPrem(long id, Map<String, List<Long>> permIdListMap) {
        // 1. 删除原有权限
        int delete = rolePermissionMapper.delete(new LambdaQueryWrapper<RolePermission>()
                .eq(RolePermission::getRid, id));
        if (delete == 0) {
            throw new RuntimeException("删除角色的权限列表失败");
        }

        // 2. 添加权限
        List<RolePermission> rolePermissionList = new ArrayList<>();
        for (Long permId : permIdListMap.get("permIdList")) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPid(permId);
            rolePermission.setRid(id);
            rolePermission.setCreateUserId(UserIdHolder.getId());
            rolePermission.setUpdateUserId(UserIdHolder.getId());
            rolePermissionList.add(rolePermission);
        }
        rolePermissionMapper.inserBatch(rolePermissionList);
        return RestResult.success();
    }

}
